package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.SingModel;
import com.hangzhou.xyj.ui.listener.DramaItemClickListener;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2015/12/28.
 * 唱戏专业榜单adapter
 */
public class TopListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "TopListAdapter";
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<SingModel> mDatas;
    private int realSize;//实际大小

    public TopListAdapter(Context context, List<SingModel> data) {
        this.mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mDatas = data;
    }

    /**
     * 更新数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/10 16:32
     */
    public void updateData(List<SingModel> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mDatas = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0:
                viewHolder = new NormalItemViewHolder(mLayoutInflater.inflate(R.layout.layout_professional_list_item_frist, viewGroup, false));
                break;
            case 1:
                viewHolder = new SecondPosItemViewHolder(mLayoutInflater.inflate(R.layout.layout_professional_list_item_second, viewGroup, false));
                break;
            default:
                viewHolder = new NormalItemViewHolder(mLayoutInflater.inflate(R.layout.layout_professional_list_item, viewGroup, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                ((NormalItemViewHolder) viewHolder).setData(0);
                break;
            case 1:
                if (realSize == 2) {
                    ((SecondPosItemViewHolder) viewHolder).setData1(1);
                    ((SecondPosItemViewHolder) viewHolder).setData1Invisible();
                } else if (realSize == 3) {
                    ((SecondPosItemViewHolder) viewHolder).setData1(1);
                    ((SecondPosItemViewHolder) viewHolder).setData2(2);
                    ((SecondPosItemViewHolder) viewHolder).setData2Invisible();
                } else if (realSize >= 4) {
                    ((SecondPosItemViewHolder) viewHolder).setData1(1);
                    ((SecondPosItemViewHolder) viewHolder).setData2(2);
                    ((SecondPosItemViewHolder) viewHolder).setData3(3);
                }
                break;
            default:
                ((NormalItemViewHolder) viewHolder).setData(position + 2);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mDatas == null) {
            return 0;
        }
        switch (mDatas.size()) {
            case 1:
                return 1;
            case 2:
                realSize = 2;
                return 2;
            case 3:
                realSize = 3;
                return 2;
            case 4:
                realSize = 4;
                return 2;
            default:
                realSize = mDatas.size();
                return mDatas.size() - 2;
        }
    }

    /**
     * 第二个位置上的View
     */
    class SecondPosItemViewHolder extends RecyclerView.ViewHolder {

        View relativeLayout;//第一图片
        MyImageView mIvImg;//背景图片
        MyTextView mTvAuthor;//作者
        MyTextView mTvDramaName;//戏剧名
        MyTextView mTvLikeNum;//点赞数

        View relativeLayout1;//第二个图片
        MyImageView mIvImg1;//背景图片
        MyTextView mTvAuthor1;//作者
        MyTextView mTvDramaName1;//戏剧名
        MyTextView mTvLikeNum1;//点赞数

        View relativeLayout2;//第三个图片
        MyImageView mIvImg2;//背景图片
        MyTextView mTvAuthor2;//作者
        MyTextView mTvDramaName2;//戏剧名
        MyTextView mTvLikeNum2;//点赞数

        public SecondPosItemViewHolder(View itemView) {
            super(itemView);
            relativeLayout = itemView.findViewById(R.id.layout_list_top_2);
            relativeLayout1 = itemView.findViewById(R.id.layout_list_top_3);
            relativeLayout2 = itemView.findViewById(R.id.layout_list_top_4);

            mIvImg = (MyImageView) relativeLayout.findViewById(R.id.miv_image);
            mTvAuthor = (MyTextView) relativeLayout.findViewById(R.id.mtv_author);
            mTvDramaName = (MyTextView) relativeLayout.findViewById(R.id.mtv_drama_name);
            mTvLikeNum = (MyTextView) relativeLayout.findViewById(R.id.mtv_like_num);

            mIvImg1 = (MyImageView) relativeLayout1.findViewById(R.id.miv_image1);
            mTvAuthor1 = (MyTextView) relativeLayout1.findViewById(R.id.mtv_author1);
            mTvDramaName1 = (MyTextView) relativeLayout1.findViewById(R.id.mtv_drama_name1);
            mTvLikeNum1 = (MyTextView) relativeLayout1.findViewById(R.id.mtv_like_num1);

            mIvImg2 = (MyImageView) relativeLayout2.findViewById(R.id.miv_image2);
            mTvAuthor2 = (MyTextView) relativeLayout2.findViewById(R.id.mtv_author2);
            mTvDramaName2 = (MyTextView) relativeLayout2.findViewById(R.id.mtv_drama_name2);
            mTvLikeNum2 = (MyTextView) relativeLayout2.findViewById(R.id.mtv_like_num2);
        }

        /**
         * @param
         * @return
         * @author yaohuix
         * @date 2016/3/8 18:50
         */
        public void setData1(int position) {
            SingModel data = mDatas.get(position);
            relativeLayout.setVisibility(View.VISIBLE);
            mIvImg.displayImage(data.getUser_head());
            mTvAuthor.setText(data.getDramaSinger());
            mTvDramaName.setText(data.getDramaName());
            mTvLikeNum.setText(String.valueOf(data.getLikeNum()));
            relativeLayout.setOnClickListener(new DramaItemClickListener(mContext, mDatas, position));
        }

        public void setData1Invisible() {
            relativeLayout1.setVisibility(View.INVISIBLE);
            relativeLayout2.setVisibility(View.INVISIBLE);
        }

        public void setData2Invisible() {
            relativeLayout2.setVisibility(View.INVISIBLE);
        }

        /**
         * @param
         * @return
         * @author yaohuix
         * @date 2016/3/8 18:59
         */
        public void setData2(int position) {
            SingModel data = mDatas.get(position);
            relativeLayout1.setVisibility(View.VISIBLE);
            mIvImg1.displayImage(data.getUser_head());
            mTvAuthor1.setText(data.getDramaSinger());
            mTvDramaName1.setText(data.getDramaName());
            mTvLikeNum1.setText(String.valueOf(data.getLikeNum()));
            relativeLayout1.setOnClickListener(new DramaItemClickListener(mContext, mDatas, position));

        }

        /**
         * @param
         * @return
         * @author yaohuix
         * @date 2016/3/8 18:59
         */
        public void setData3(int position) {
            SingModel data = mDatas.get(position);
            relativeLayout2.setVisibility(View.VISIBLE);
            mIvImg2.displayImage(data.getUser_head());
            mTvAuthor2.setText(data.getDramaSinger());
            mTvDramaName2.setText(data.getDramaName());
            mTvLikeNum2.setText(String.valueOf(data.getLikeNum()));
            relativeLayout2.setOnClickListener(new DramaItemClickListener(mContext, mDatas, position));
        }
    }

    /**
     * 普通item
     */
    class NormalItemViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout relativeLayout;//item所在的布局
        MyImageView mIvImg;//背景图片
        MyTextView mTvAuthor;//作者
        MyTextView mTvDramaName;//戏剧名
        MyTextView mTvLikeNum;//点赞数

        public NormalItemViewHolder(View itemView) {
            super(itemView);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl_first);
            mIvImg = (MyImageView) itemView.findViewById(R.id.miv_image);
            mTvAuthor = (MyTextView) itemView.findViewById(R.id.mtv_author);
            mTvDramaName = (MyTextView) itemView.findViewById(R.id.mtv_drama_name);
            mTvLikeNum = (MyTextView) itemView.findViewById(R.id.mtv_like_num);
        }

        public void setData(int position) {
            SingModel data = mDatas.get(position);
            if (position != 0) {
                mTvLikeNum.setVisibility(View.INVISIBLE);
            } else {
                mTvLikeNum.setVisibility(View.VISIBLE);
                mTvLikeNum.setText(String.valueOf(data.getLikeNum()));
            }
//            relativeLayout.setVisibility(View.VISIBLE);
            mIvImg.displayImage(data.getUser_head());
            mTvAuthor.setText(data.getDramaSinger());
            mTvDramaName.setText(data.getDramaName());
            mTvLikeNum.setText(String.valueOf(data.getLikeNum()));
            itemView.setOnClickListener(new DramaItemClickListener(mContext, mDatas, position));
        }
    }
}
